package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type ProductCustomFieldListRowHandler interface {
	ListProductCustomFieldListRow() gin.HandlerFunc
	GetProductCustomFieldListRow() gin.HandlerFunc
	AddProductCustomFieldListRow() gin.HandlerFunc
	EditProductCustomFieldListRow() gin.HandlerFunc
	DeleteProductCustomFieldListRow() gin.HandlerFunc
	BatchDeleteProductCustomFieldListRow() gin.HandlerFunc
}

type productCustomFieldListRowHandler struct {
	somsClient soms.Client
}

func (h *productCustomFieldListRowHandler) ListProductCustomFieldListRow() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListProductCustomFieldListRowDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListProductCustomFieldListRow(util.GetRPCContext(ctx), &api.ListProductCustomFieldListRowRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.ProductCustomFieldListRowData{
				ColumnId: req.ColumnId,
	JsonValue: req.JsonValue,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *productCustomFieldListRowHandler) GetProductCustomFieldListRow() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetProductCustomFieldListRow(util.GetRPCContext(ctx), &api.ProductCustomFieldListRowData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *productCustomFieldListRowHandler) AddProductCustomFieldListRow() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.ProductCustomFieldListRowData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.ProductCustomFieldListRowData{
            Id:         data.Id,
            ColumnId: data.ColumnId,
	JsonValue: data.JsonValue,
	
        }
		err := h.somsClient.AddProductCustomFieldListRow(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldListRowHandler) EditProductCustomFieldListRow() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.ProductCustomFieldListRowData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.ProductCustomFieldListRowData{
            Id:         id,
            ColumnId: data.ColumnId,
	JsonValue: data.JsonValue,
	
        }
		if err = h.somsClient.EditProductCustomFieldListRow(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldListRowHandler) DeleteProductCustomFieldListRow() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteProductCustomFieldListRow(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldListRowHandler) BatchDeleteProductCustomFieldListRow() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteProductCustomFieldListRowRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteProductCustomFieldListRow(util.GetRPCContext(ctx), &api.BatchDeleteProductCustomFieldListRowRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

